<template>
	<view v-if="pageShow" class="container">
		<scroll-view scroll-y="true" style="height: 100%;" refresher-threshold="50" :refresher-background="'#F7F7F7'"
			:refresher-enabled="true" :refresher-triggered="isRefreshing" @refresherrefresh="refresherrefresh"
			@scrolltolower="loadMore">
			<view class="my-demand">
				<view class="demand-item" v-for="(item, index) in demandList" :key="index" @click="goDemandDetail(item)">
					<view class="header">
						<view class="header-left">
							<image src="https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/enterprise-service/demand/demand-icon.png" class="icon"></image>
							<text style="margin-left: 20rpx;">{{item.name}}</text>
						</view>
						<view class="header-right">
							<text :class="item.status===0?'color1':'color2'">{{item.status===0?'进行中':item.status===1?'已完成':'已关闭'}}</text>
						</view>
					</view>
					<view class="content">
						<view class="content-item">
							<text class="cuIcon-title"></text>
							<text style="margin-left: 30rpx;">发布时间：</text>
							<text>{{item.time}}</text>
						</view>
						<view class="content-item">
							<text class="cuIcon-title"></text>
							<text style="margin-left: 30rpx;">服务地址：</text>
							<text>{{item.city_name}}</text>
						</view>
					</view>
					<view v-if="item.status===0 || item.status===2" class="bottom-btn">
						<view class="btn" @click.stop="editDemand(item)">
							{{item.status===0?'关闭':item.status===2?'删除':''}}
						</view>
					</view>
				</view>
				<uni-load-more v-if="demandList.length > 0" :status="listStatus"></uni-load-more>
				<view v-if="demandList.length === 0" class="empty-wrap" >
				  <image class="empty-img" src="https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/coupon/contentVanshi.png"></image>
				  <text class="tips">暂无内容</text>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import demandApi from "@/api/demand.js"
	export default {
		data() {
			return {
				demandList: [],
				//分页参数
				pageShow: false,
				page: 1,
				pageSize: 10,
				isRefreshing: false,
				listStatus: 'nomore'
			}
		},
		onLoad() {
			
		},
		onShow() {
			this.getList()
		},
		methods: {
			async getList() {
				this.listStatus = 'loading'
				uni.showLoading({
					title: '加载中'
				})
				let obj = {
					page: this.page,
					pagesize: this.pageSize
				}
				let res = await demandApi.getMyDemand(obj)
				if(res.code === 1) {
					let arr = []
					if (this.page == 1) {
						this.demandList = []
					}
					let newList = res.data
					if (newList.length >= this.pageSize) {
						this.listStatus = 'more'
					} else {
						this.listStatus = 'nomore'
					}
					let arr1 = arr.concat(newList)
					arr1.forEach(val => {
						let obj = {
							id: val.id,
							name: val.name,
							time: val.createtime_text,
							city_name: val.city_name,
							status: val.status
						}
						this.demandList.push(obj)
					})
					this.pageShow = true
					this.isRefreshing = false
					uni.hideLoading()
				}
			},
			
			//跳转需求详情
			goDemandDetail(val) {
				uni.navigateTo({
					url: '/pagesDemand/demandDetail/demandDetail?id=' + val.id
				})
			},
			
			
			//关闭或删除需求
			editDemand(val) {
				let that = this
				if(val.status === 0) {
					uni.showModal({
						cancelColor: "#999",
						cancelText: '再想想',
						confirmColor: "#0177fd",
						confirmText: '确认关闭',
						content: '确认关闭此需求吗？',
						success:function(ots){
							if(ots.confirm) {
								demandApi.closeDemand({id: val.id}).then(res => {
									if(res.code === 1) {
										that.$common.msgShow('关闭成功')
										that.getList()
									}else {
										that.$common.msgShow(res.msg)
									}
								})
							}
						}
					})
				}else {
					demandApi.delDemand({id: val.id}).then(res => {
						if(res.code === 1) {
							that.$common.msgShow('删除成功')
							that.getList()
						}else {
							that.$common.msgShow(res.msg)
						}
					})
				}
			},
			
			//下拉刷新
			refresherrefresh() {
				this.isRefreshing = true;
				this.page = 1
				this.getList()
			},
			
			//上拉加载
			loadMore() {
				if (this.listStatus == 'more') {
					this.page += 1
					this.getList()
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
 @import 'myDemand.scss';
</style>
